//
//  Bookmark.m
//  We365
//
//  Created by Pham Khuong Duy on 12/13/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "Bookmark.h"
#import "DBManager.h"

@implementation Bookmark

- (id)init
{
    self = [super init];
    if (self) {
        list = [[NSMutableArray alloc] init];
    }
    
    return self;
}

- (void)getAllFavourites
{
    [DBManager openDB];
    [self.list removeAllObjects];
    char *getAll = "SELECT * FROM BOOKMARK;";
    sqlite3_stmt *stmt;
    sqlite3_prepare_v2([DBManager getDB], getAll, -1, &stmt, nil);
    while (sqlite3_step(stmt) == SQLITE_ROW) {
        Item *item = [[Item alloc] init];
        char *itemID = (char *)sqlite3_column_text(stmt, 0);
        item.ItemID = [NSString stringWithUTF8String:itemID];
        char *itemName = (char *)sqlite3_column_text(stmt, 1);
        item.ItemName = [NSString stringWithUTF8String:itemName];
        item.Rating = (float)sqlite3_column_double(stmt, 2);
        char *reference = (char *)sqlite3_column_text(stmt, 3);
        item.Reference = [NSString stringWithUTF8String:reference];
        char *address = (char *)sqlite3_column_text(stmt, 4);
        item.Address = [NSString stringWithUTF8String:address];
        item.Latitude = (float)sqlite3_column_double(stmt, 5);
        item.Longitude = (float)sqlite3_column_double(stmt, 6);
        item.Distance = (float)sqlite3_column_double(stmt, 7);
        char *phone = (char *)sqlite3_column_text(stmt, 8);
        item.Phone = [NSString stringWithUTF8String:phone];
        char *website = (char *)sqlite3_column_text(stmt, 9);
        item.Website = [NSString stringWithUTF8String:website];
        [self.list addObject:item];
        [item release];
    }
    sqlite3_finalize(stmt);
    [DBManager closeDB];
}

- (void)addFavourite:(Item *)item
{
    [DBManager openDB];
    sqlite3_stmt *stmt;
    char *insert = "INSERT INTO BOOKMARK (ITEM_ID, ITEM_NAME, RATING, REFERENCE, ADDRESS, LAT, LONG, DISTANCE, PHONE, WEBSITE) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?);";
    sqlite3_prepare_v2([DBManager getDB], insert, -1, &stmt, nil);
    sqlite3_bind_text(stmt, 1, [item.ItemID UTF8String], -1, NULL);
    sqlite3_bind_text(stmt, 2, [item.ItemName UTF8String], -1, NULL);
    sqlite3_bind_double(stmt, 3, [NSDecimalNumber numberWithFloat:item.Rating].doubleValue);
    sqlite3_bind_text(stmt, 4, [item.Reference UTF8String], -1, NULL);
    sqlite3_bind_text(stmt, 5, [item.Address UTF8String], -1, NULL);
    sqlite3_bind_double(stmt, 6, [NSDecimalNumber numberWithFloat:item.Latitude].doubleValue);
    sqlite3_bind_double(stmt, 7, [NSDecimalNumber numberWithFloat:item.Longitude].doubleValue);
    sqlite3_bind_double(stmt, 8, [NSDecimalNumber numberWithFloat:item.Distance].doubleValue);    
    sqlite3_bind_text(stmt, 9, [item.Phone UTF8String], -1, NULL);
    sqlite3_bind_text(stmt, 10, [item.Website UTF8String], -1, NULL);
    sqlite3_step(stmt);
    sqlite3_finalize(stmt);
    [DBManager closeDB];
}

- (void)removeFavourite:(NSString *)itemID
{
    [DBManager openDB];
    char *delete = "DELETE FROM BOOKMARK WHERE ITEM_ID = ?;";
    sqlite3_stmt *stmt;
    sqlite3_prepare_v2([DBManager getDB], delete, -1, &stmt, nil);
    sqlite3_bind_text(stmt, 1, [itemID UTF8String], -1, NULL);
    sqlite3_step(stmt);
    sqlite3_finalize(stmt);
    [DBManager closeDB];
}

- (BOOL)isFavourite:(NSString *)itemID
{
    NSDecimalNumber *result = nil;
    [DBManager openDB];
    char *select = "SELECT COUNT(*) FROM BOOKMARK WHERE ITEM_ID = ?;";
    sqlite3_stmt *stmt;
    sqlite3_prepare_v2([DBManager getDB], select, -1, &stmt, nil);
    sqlite3_bind_text(stmt, 1, [itemID UTF8String], -1, NULL);
    while (sqlite3_step(stmt) == SQLITE_ROW) {
        result = (NSDecimalNumber *)[NSDecimalNumber numberWithDouble:sqlite3_column_double(stmt, 0)];
    }
    sqlite3_finalize(stmt);
    [DBManager closeDB];
    
    if([result intValue] == 0)
        return NO;
    else
        return YES;
}

@end
